#include"Mushroom.h"
#define mushroom_speed 0.25f
#define mushroom_fall 0.04f
CMushroom::CMushroom(int kind, int _x, int _y, int _w, int _h)
{
	x=_x;
	y=_y;
	width=_w;
	height=_h;
	if(kind==77) mushroom=new CSprite(true,33,0,_w,_h,0,0,0);
	else mushroom=new CSprite(true,0,0,_w,_h,0,0,0); //44
	isshow=true;
	isactive=true;
	isfalling=false;
	isauto=false;
	istouchx=false;
	isover=false;
}
CMushroom::~CMushroom()
{
	if(mushroom!=NULL) delete mushroom;
}
int CMushroom::CollisionWithMario(RECT marioRect)
{
	if(isover==true) return 0;
	RECT mushroomRect=mushroom->getRECT(x,y);
	RECT dest;
	if(IntersectRect(&dest,&mushroomRect,&marioRect))
	{
		if(isshow==false)
		{
			isactive=true;
			isshow=true;
			return 0;
		}
		else 
		{
			if(isauto)
			{
				isshow=false;
				isover=true;
				return 1;
			}
		}
	}
	return 0;
}	
void CMushroom::Move(CStaticObjects *StaticObject,RECT screen)
{
	if(isshow==false) return;
	RECT mushroomRect=mushroom->getRECT(x,y);
	RECT objectRect;
	int kq=0;
	mushroomRect.bottom-=10;
	
	StaticObject->tree->Collision(StaticObject->tree->root,screen,mushroomRect,&kq,&objectRect);
	if(kq!=0)
	{
		if(moveleft==false) moveleft=true;
		else moveleft=false;
		x-=2;
		istouchx=true;
	}
	mushroomRect=mushroom->getRECT(x,y);
	mushroomRect.top=mushroomRect.bottom-10;
	mushroomRect.bottom+=5;
	StaticObject->tree->Collision(StaticObject->tree->root,screen,mushroomRect,&kq,&objectRect);
	if(kq!=0) 
	{
		isfalling=false;
		vy=0;
		if(istouchx==false) 
		{
			y=objectRect.top-height+2;
		}
	}
	else isfalling=true;
}
void CMushroom::Render(LPD3DXSPRITE sprite, LPDIRECT3DTEXTURE9 image, int time, int xcamara)
{
	if(isactive==true)
	{
		y-=32;
		isactive=false;
		isauto=true;
	}
	if(isauto==true)
	{
		if(moveleft==true) vx=-mushroom_speed;
		else vx=mushroom_speed;
		if(isfalling==true) vy=mushroom_fall;
		else vy=0;
		x+=vx*time;
		y+=vy*time*time;
	}
	if(isshow==true)
	{
		mushroom->Render(sprite,image,x-xcamara,y);
	}
}